﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcmyjobs.Web.Controls
{
    public partial class UserAndEnterpriseLogin : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["user_name"] != null)
                {
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    this.Label1.Text = "欢迎您：" + Session["user_name"].ToString();
                }

                if (Session["enterprise_name"] != null)
                {
                    this.pnl_enterprise_login.Visible = false;
                    this.pnl_enterprise_logined.Visible = true;
                    this.lbl_enterprise.Text = "欢迎您：" + Session["enterprise_name"].ToString();

                    if (Session["user_name"] == null)
                    {
                        this.islogin.CssClass = "1";
                       

                    }
                }
                else
                {
                    this.pnl_enterprise_login.Visible = true;
                    this.pnl_enterprise_logined.Visible = false;
                }
                
            }
            /*
            if (Session["login_panel"] != null)
            {
                if (Session["login_panel"].ToString() == "1")
                {
                    this.islogin.CssClass = "1";
                    Session["login_panel"] = null;
                }
            }
             */

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                // add by liuhui 如果企业登录则个人不能登录
                if (Session["enterprise_bid"] != null )
                {
                    //Maticsoft.Common.MessageBox.Show(Page, "您已经登录单位账号。请先退出，才能登录个人账号！");
                    this.Lbl_Tishi2.Text = "您已经登录单位账号。请先退出，才能登录个人账号！";
                    this.islogin.CssClass = "0";
                    return;
                }
                string name = this.TextBox1.Text.Trim();
                string pwd = this.TextBox2.Text.Trim();
                string checkcode = this.TextBox3.Text.Trim();
                if (Session["CheckCode"] != null)
                {
                    if (checkcode.ToUpper() == Session["CheckCode"].ToString())
                    {
                        lcmyjobs.BLL.user_basic bad = new BLL.user_basic();
                        string mname = "", time = "";
                        pwd = myPublic.GetMd5(pwd);
                        int id = bad.UserLogin(name, pwd, out mname, out time);
                        if (id != 0)
                        {
                            Session["user_id"] = id.ToString();
                            Session["user_name"] = mname;
                            //Response.Redirect("user_main.aspx");
                            this.Label1.Text = "欢迎您：" + mname;
                            this.Panel1.Visible = false;
                            this.Panel2.Visible = true;

                            // 判断用户是否注册完成 add by liuhui 131102
                            lcmyjobs.BLL.user_basic bud = new BLL.user_basic();
                            DataSet ds = bud.GetUserMain(id);

                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                if (ds.Tables[0].Rows[0]["isregok"].ToString() == "3")
                                {
                                    Session["user_id_reg"] = Session["user_id"];
                                    Session["user_name_reg"] = Session["user_name"];
                                    Session["user_id"] = null;
                                    Session["user_name"] = null;
                                    Response.Redirect("../users/reg_edu.html");

                                    //Maticsoft.Common.MessageBox.ShowAndRedirect(this.Page, "您还没有完成注册，请先完成注册！", "../users/reg_edu.html");
                                    
                                }
                                else if (ds.Tables[0].Rows[0]["isregok"].ToString() == "2")
                                {
                                    Session["user_id_reg"] = Session["user_id"];
                                    Session["user_name_reg"] = Session["user_name"];
                                    Session["user_id"] = null;
                                    Session["user_name"] = null;
                                    Response.Redirect("../users/reg_job.html");
                                    //Maticsoft.Common.MessageBox.ShowAndRedirect(this.Page, "您还没有填写简历，请先填写简历！", "../users/reg_job.html");
                                }
                            }
                        }
                        else
                        {
                            //Maticsoft.Common.MessageBox.Show(Page, "用户名或密码不对！");
                            this.Lbl_Tishi2.Text = "用户名或密码不对！";
                            this.islogin.CssClass = "0";
                        }

                    }
                    else
                    {
                        //Maticsoft.Common.MessageBox.Show(Page, "验证码不对！");
                        this.Lbl_Tishi2.Text = "验证码不对！";
                        this.islogin.CssClass = "0";
                    }
                }
                else
                {
                    //Maticsoft.Common.MessageBox.Show(Page, "验证码不对！");
                    this.Lbl_Tishi2.Text = "验证码不对！";
                    this.islogin.CssClass = "0";
                 }
            }

            
        }

        protected void btn_enLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                // add by liuhui 如果个人登录则企业不能登录
                if (Session["user_id"] != null)
                {
                    //Maticsoft.Common.MessageBox.Show(Page, "您已经登录个人账号。请先退出，才能登录单位账号！");
                    this.Lbl_Tishi1.Text = "您已经登录个人账号。请先退出，才能登录单位账号！";
                    this.islogin.CssClass = "1";
                    return;
                }
                string login = this.txtname.Text;
                string pwd = this.txtpass.Text;
                string checkcode = this.txt_code.Text.Trim();
                if (Session["CheckCode"] != null)
                {
                    if (checkcode.ToUpper() == Session["CheckCode"].ToString())
                    {
                        lcmyjobs.BLL.enterprise_basic eb = new BLL.enterprise_basic();
                        int enterprise_bid = 0;
                        pwd = myPublic.GetMd5(pwd);
                        DataSet ds = eb.Check_login(login, pwd, out enterprise_bid);

                        if (enterprise_bid != 0)
                        {
                            Session["enterprise_bid"] = enterprise_bid;

                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                Session["lastlogin"] = ds.Tables[0].Rows[0][0];//上次登陆时间
                                Session["enterprise_name"] = ds.Tables[0].Rows[0][1];//企业名称
                            }

                            this.pnl_enterprise_login.Visible = false;
                            this.pnl_enterprise_logined.Visible = true;

                            this.lbl_enterprise.Text = "欢迎您：" + Session["enterprise_name"].ToString();
                            this.islogin.CssClass = "1";
                        }
                        else
                        {
                            //Maticsoft.Common.MessageBox.Show(Page, "用户名或密码不对！");
                            this.Lbl_Tishi1.Text = "用户名或密码不对！";
                            this.islogin.CssClass = "1";
                        }
                    }
                    else
                    {
                        //Maticsoft.Common.MessageBox.Show(Page, "验证码不对！");
                        this.Lbl_Tishi1.Text = "验证码不对！";
                        this.islogin.CssClass = "1";
                    }
                }
                else
                {
                    //Maticsoft.Common.MessageBox.Show(Page, "验证码不对！");
                    this.Lbl_Tishi1.Text = "验证码不对！";
                    this.islogin.CssClass = "1";
                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("~/index.html");
        }
    }
}